<?php
/**
 * Created by PhpStorm.
 * User: Jesse
 * Date: 2018/6/7
 * Time: 16:18
 */

namespace App\Handlers;

use QL\QueryList;

class SchoolJobHandler
{
    public static function jobs()
    {
        // 默认取最近20条记录
        $baseUrl = 'http://www.baoan.gov.cn/jyj/zwfw/zpxx/';
        $urls = [
            // $baseUrl . "index_1.html",
            $baseUrl . "index.html",
        ];
        $queryList = new QueryList;
        $rules = [
            'text' => ['a','text'],
            'date' => ['span', 'text'],
            'page' => ['a', 'href', '', function($content) {
                if (strpos($content, './') !== false) {
                    $content = substr($content, strpos($content, 't') + 1, -5);
                }
                return $content;
            }],
            'link' => ['a','href','',function($content) use ($baseUrl){
                if (strpos($content, './') !== false) {
                    $content = substr($content, 2);
                }
                return $content;
            }],
        ];
        $query = $queryList->rules($rules)->range('.zx_ml_list .list_name');

        $data = $arr = [];
        foreach ($urls as $url) {
            $arr = $query->get($url)->query()->getData()->all();
            $data = array_merge($arr, $data);
            unset($arr);
        }

        return $data;
    }
}
